﻿using MusicPlayerPlus.Interface.Model.Muisc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MusicPlayerPlus.Core
{
    public class CoreData
    {
        #region Dictionary

        private static Dictionary<Guid, object> publicData = new Dictionary<Guid, object>();
        /// <summary>
        /// 全局通用数据
        /// </summary>
        public static Dictionary<Guid, object> PublicData
        {
            get { return publicData; }
            set { publicData = value; }
        }
        /// <summary>
        /// 获取指定guid的数据
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static object GetDictionaryData(Guid guid)
        {
            if (PublicData.ContainsKey(guid))
            {
                return PublicData[guid];
            }
            return null;
        }
        /// <summary>
        /// 添加以guid为key的数据
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool AddDictionaryData(Guid guid, object data)
        {
            if (PublicData.ContainsKey(guid))
            {
                return false;
            }
            PublicData.Add(guid, data);
            Dictionary_Change_Action(guid, data);
            return true;
        }
        /// <summary>
        /// 改变指定guid的数据
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool ChangeDictionaryData(Guid guid, object data)
        {
            if (PublicData.ContainsKey(guid))
            {
                PublicData[guid] = data;
                Dictionary_Change_Action(guid, data);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除指定key的数据
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static bool DeleteDictionaryData(Guid guid)
        {
            if (PublicData.ContainsKey(guid))
            {
                Dictionary_Change_Action(guid, null);
                return PublicData.Remove(guid);
            }
            return false;
        }

        #endregion

        #region PlayList

        private static Dictionary<Guid, PlayList> playListsData = null;
        /// <summary>
        /// 所有的播放列表
        /// </summary>
        public static Dictionary<Guid, PlayList> PlayListsData
        {
            get
            {
                if (playListsData == null)
                {
                    playListsData = new Dictionary<Guid, PlayList>();
                }

                return playListsData;
            }
            set { playListsData = value; }
        }

        /// <summary>
        /// 创建默认播放列表
        /// </summary>
        private static void CreateDefaultPlayList()
        {
            if (!playListsData.ContainsKey(StaticData.Default_PlayList))//不存在默认播放列表 就创建
            {
                try
                {
                    //创建默认播放列表
                    PlayList defaultPlayList = new PlayList()
                    {
                        ID = StaticData.Default_PlayList,
                        Name = "默认播放列表",
                        Muisic = new Dictionary<Guid, MusicItem>()
                    };
                    playListsData.Add(StaticData.Default_PlayList, defaultPlayList);
                    PlayList_Change_Action(defaultPlayList.ID, defaultPlayList);
                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// 添加数据到PlayList
        /// </summary>
        /// <param name="main"></param>
        /// <param name="add"></param>
        private static void AddToPlayList(PlayList main, PlayList add)
        {
            if (main == null || add == null || main.Muisic == null || add.Muisic == null)
            {
                return;
            }
            foreach (var item in add.Muisic)
            {
                if (!main.Muisic.ContainsKey(item.Key))
                {
                    main.Muisic.Add(item.Key, item.Value);
                }
            }
        }

        /// <summary>
        /// 获取所有的播放列表
        /// </summary>
        /// <returns></returns>
        public static IList<PlayList> GetPlayList()
        {
            IList<PlayList> list = new List<PlayList>();
            foreach (var item in PlayListsData)
            {
                list.Add(item.Value);
            }

            return list;
        }
        /// <summary>
        /// 添加播放列表
        /// </summary>
        /// <param name="list"></param>
        public static bool AddPlayList(PlayList list)
        {
            if (list == null)
            {
                return false;
            }
            if (PlayListsData.ContainsKey(list.ID))
            {
                AddToPlayList(PlayListsData[list.ID], list);
            }
            try
            {
                PlayListsData.Add(list.ID, list);
            }
            catch (Exception ex)
            {
            }
            PlayList_Change_Action(list.ID, list);
            return true;
        }
        /// <summary>
        /// 改变播放列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool ChangePlayList(PlayList list)
        {
            if (list == null)
            {
                return false;
            }
            if (PlayListsData.ContainsKey(list.ID))
            {
                PlayListsData[list.ID] = list;
                PlayList_Change_Action(list.ID, list);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除播放列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool DeletePlayList(PlayList list)
        {
            if (list == null)
            {
                return false;
            }
            if (PlayListsData.ContainsKey(list.ID))
            {
                PlayList_Change_Action(list.ID, null);
                return PlayListsData.Remove(list.ID);
            }
            return false;
        }

        /// <summary>
        /// 获取指定的播放列表
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static PlayList GetPlayListForGuid(Guid guid)
        {
            if (PlayListsData.ContainsKey(guid))
            {
                return PlayListsData[guid];
            }
            return null;
        }

        /// <summary>
        /// 获取播放中的播放列表
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static PlayList GetPlayingPlayList()
        {
            return PlayListData;
        }

        #endregion

        #region MusicList

        private static Dictionary<Guid, MusicItem> musicList = null;
        /// <summary>
        /// 播放中的列表
        /// </summary>
        public static Dictionary<Guid, MusicItem> MusicList
        {
            get
            {
                if (musicList == null)
                {
                    if (!PlayListsData.ContainsKey(StaticData.Default_PlayList))
                    {
                        CreateDefaultPlayList();
                    }
                    PlayList defaultPlayList = PlayListsData[StaticData.Default_PlayList];
                    PlayListData = defaultPlayList;
                    musicList = PlayListData.Muisic;
                }
                return musicList;
            }
            set { musicList = value; }
        }
        /// <summary>
        /// 正在播放的列表
        /// </summary>
        public static PlayList PlayListData { get; set; }
        /// <summary>
        /// 获取播放中的列表
        /// </summary>
        /// <returns></returns>
        public static IList<MusicItem> GetMusicList()
        {
            IList<MusicItem> list = new List<MusicItem>();
            foreach (var item in MusicList)
            {
                list.Add(item.Value);
            }

            return list;
        }
        /// <summary>
        /// 添加音乐到播放中的列表
        /// </summary>
        /// <param name="list"></param>
        public static void AddMusicList(IList<MusicItem> list)
        {
            foreach (var item in list)
            {
                if (!MusicList.ContainsKey(item.ID))
                {
                    MusicList.Add(item.ID, item);
                }
            }
            MuiscList_Change_Action(MusicList);
            if (PlayListData != null)
            {
                PlayList_Change_Action(PlayListData.ID, PlayListData);
            }
        }
        /// <summary>
        /// 改变播放中的列表
        /// </summary>
        /// <param name="list"></param>
        public static void ChangeMusicList(Guid guid)
        {
            if (!PlayListsData.ContainsKey(guid))
            {
                return;
            }
            PlayList list = PlayListsData[guid];
            PlayListData = list;
            MusicList = PlayListData.Muisic;
            PlayMusic = null;
            MuiscList_Change_Action(MusicList);
        }

        #endregion

        #region Music

        /// <summary>
        /// 播放中的歌曲
        /// </summary>
        public static MusicItem PlayMusic { get; set; }

        /// <summary>
        /// 获取正在播放的歌曲
        /// </summary>
        /// <returns></returns>
        public static MusicItem GetPlayMusic()
        {
            return PlayMusic;
        }

        /// <summary>
        /// 获取正在播放的歌曲
        /// </summary>
        /// <returns></returns>
        public static void SetPlayMusic(MusicItem item)
        {
            PlayMusic = item;
            PlayMusic_Change_Action(PlayMusic);
        }

        /// <summary>
        ///  添加歌曲
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool AddMusic(MusicItem item)
        {
            if (MusicList.ContainsKey(item.ID))
            {
                return false;
            }
            MusicList.Add(item.ID, item);
            MuiscList_Change_Action(MusicList);
            return true;
        }
        /// <summary>
        /// 改变歌曲
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool ChangeMusic(MusicItem item)
        {
            if (MusicList.ContainsKey(item.ID))
            {
                MusicList[item.ID] = item;
                MuiscList_Change_Action(MusicList);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除歌曲
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool DeleteMusic(MusicItem item)
        {
            if (MusicList.ContainsKey(item.ID))
            {
                bool data = MusicList.Remove(item.ID);
                MuiscList_Change_Action(MusicList);
                return data;
            }
            return false;
        }

        #endregion

        #region OrientationAction
        //
        //以Key为键，接收端以一个属性为Key，对应一个方法 用于核心这边通知
        //添加到核心时key不存在创建，存在则叠加
        //
        //发送端 以双Key 传输， MainKey对应传输 事件， 次要Key用于插件内部区分

        static Dictionary<string, IList<Action<string, object>>>
            _orientationAction =
            new Dictionary<string, IList<Action<string, object>>>();
        /// <summary>
        /// 定向方法 通道
        /// </summary>
        public static Dictionary<string, IList<Action<string, object>>>
            OrientationAction
        {
            get
            {
                return _orientationAction;
            }
            set
            {
                _orientationAction = value;
            }
        }

        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="action"></param>
        public static void AddToOrientationAction(string key, Action<string, object> action)
        {
            if (OrientationAction.ContainsKey(key))//存在则追加
            {
                OrientationAction[key].Add(action);
            }
            else
            {
                IList<Action<string, object>> actionList = new List<Action<string, object>>();
                actionList.Add(action);
                OrientationAction.Add(key, actionList);
            }
        }
        /// <summary>
        ///  定向传输数据
        /// </summary>
        /// <param name="mainKey"></param>
        /// <param name="datakey"></param>
        /// <param name="data"></param>
        public static void TransmitOrientationAction(string mainKey, string datakey, object data)
        {
            if (OrientationAction.ContainsKey(mainKey))//存在则追加
            {
                foreach (var item in OrientationAction[mainKey])
                {
                    try
                    {
                        if (item != null)
                        {
                            item(datakey, data);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
        #endregion

        #region EventAction

        /// <summary>
        /// Dictionary数据改变时
        /// </summary>
        public static Action<Guid, object> Dictionary_Change { get; set; }
        /// <summary>
        /// Dictionary数据改变时
        /// </summary>
        private static void Dictionary_Change_Action(Guid guid, object obj)
        {
            if (Dictionary_Change != null)
            {
                Dictionary_Change(guid, obj);
            }
        }

        /// <summary>
        /// 播放列表改变时
        /// </summary>
        public static Action<Guid, PlayList> PlayList_Change { get; set; }
        /// <summary>
        /// 播放列表改变时
        /// </summary>
        private static void PlayList_Change_Action(Guid guid, PlayList list)
        {
            if (PlayList_Change != null)
            {
                PlayList_Change(guid, list);
            }
        }
        /// <summary>
        /// 播放中的列表改变时
        /// </summary>
        public static Action<Dictionary<Guid, MusicItem>> MuiscList_Change { get; set; }
        /// <summary>
        /// 播放中的列表改变时
        /// </summary>
        private static void MuiscList_Change_Action(Dictionary<Guid, MusicItem> list)
        {
            if (MuiscList_Change != null)
            {
                MuiscList_Change(list);
            }
        }
        /// <summary>
        /// 播放的歌曲改变时
        /// </summary>
        public static Action<MusicItem> PlayMusic_Change { get; set; }
        /// <summary>
        /// 播放的歌曲改变时
        /// </summary>
        private static void PlayMusic_Change_Action(MusicItem item)
        {
            if (PlayMusic_Change != null)
            {
                PlayMusic_Change(item);
            }
        }

        #endregion
    }
}